﻿using Kavaa.Modules.PortailMedical.Datalayer;
using Kavaa.Modules.PortailMedical.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Kavaa.Modules.PortailMedical.Repository
{
    public static class Cabinet
    {
        public static Cliniques DecouvrirCliniques(int acteId, int villeId, int userId)
        {

            Accreditations Accreditations = AccreditationDAO.GetAccreditationsByUserByActe(acteId, userId);

            Cliniques Cliniques = new Cliniques();
            Cliniques Cliniques1 = new Cliniques();
            Boolean etat = false;
            int i = 0;
            Cliniques = CliniqueDAO.DecouvrirCliniques(acteId, villeId);
            if (Cliniques.Count != 0)
            {
                foreach (Kavaa.Modules.PortailMedical.Entity.Clinique clinique in Cliniques)
                {
                    while (etat == false && i < Accreditations.Count)
                    {
                        if (Accreditations.ElementAt(i).Clinique.Id == clinique.Id)
                        {
                            etat = true;
                        }
                        i++;
                    }
                    if (etat == false)
                    {
                        Cliniques1.Add(clinique);
                    }

                    else { etat = false; }

                    i = 0;
                }
                return Cliniques1;
            }

            return Cliniques1;

        }
        public static void AddAccreditation(int MedecinId, int CliniqueId, int ActeId)
        {
            AccreditationDAO.AddAccreditation(MedecinId, CliniqueId, ActeId);
        }
        public static Accreditations GetAccreditationsEnCoursByUser(int userId)
        {
            Accreditations Accreditations = AccreditationDAO.GetAccreditationsByUser(userId);
            Accreditations AccreditationsEnCours = new Accreditations(); 
            //l'état des accréditations accordées est à 1
            foreach (Accreditation Accredit in Accreditations)
            {
                if (Accredit.Etat_Clinique == 0)
                {
                    AccreditationsEnCours.Add(Accredit);
                }
            }
            return AccreditationsEnCours;

        }
        public static Accreditations GetAccreditationsAccordeesByUser(int userId)
        {
            Accreditations Accreditations = AccreditationDAO.GetAccreditationsByUser(userId);
            Accreditations AccreditationsAccordees = new Accreditations();
            //l'état des accréditations accordées est à 1
            foreach (Accreditation Accredit in Accreditations)
            {
                if (Accredit.Etat_Clinique == 1)
                {
                    AccreditationsAccordees.Add(Accredit);
                }
            }
            return AccreditationsAccordees;
        
        }
        public static Accreditations GetAccreditationsRefuseesByUser(int userId)
        {
            Accreditations Accreditations = AccreditationDAO.GetAccreditationsByUser(userId);
            Accreditations AccreditationsRefusees = new Accreditations(); 
            //l'état des accréditations accordées est à 1
            foreach (Accreditation Accredit in Accreditations)
            {
                if (Accredit.Etat_Clinique == 2)
                {
                    AccreditationsRefusees.Add(Accredit);
                }
            }
            return AccreditationsRefusees;

        }


        public static Cliniques DecouvrirCliniquesAccredit(int acteId, int userId)
        {

            Accreditations Accreditations = AccreditationDAO.GetAccreditationsByUserByActe(acteId, userId);

            Cliniques Cliniques = new Cliniques();
            Cliniques Cliniques1 = new Cliniques();
            Boolean etat = false;
            int i = 0;
            Cliniques = CliniqueDAO.DecouvrirCliniquesByActe(acteId);
            if (Cliniques.Count != 0)
            {
                foreach (Kavaa.Modules.PortailMedical.Entity.Clinique clinique in Cliniques)
                {
                    while (etat == false && i < Accreditations.Count)
                    {
                        if (Accreditations.ElementAt(i).Clinique.Id == clinique.Id && Accreditations.ElementAt(i).Etat_Clinique == 1)
                        {
                            Cliniques1.Add(clinique);
                            etat = true;
                        }
                        i++;
                    }


                    etat = false;

                    i = 0;
                }
                return Cliniques1;
            }

            return Cliniques1;

        }
        public static Reservations GetReservbloquees(int CliniqueId, int acteId)
        {
            Categories Categories = CategorieDAO.Getblocs(CliniqueId, acteId);
            Reservations Reservations2 = new Reservations();
            DateTime DateDebut;
            DateTime DateFin;

            Categories Autresblocs = new Categories();
            for (int j = 1; j < Categories.Count; j++)
            {
                Autresblocs.Add(Categories.ElementAt(j));
            }


            foreach (Reservation Reservation in ReservationDAO.GetReservationsbloc(Categories.ElementAt(0).Id))
            {
                DateDebut = Reservation.DateDebut;
                DateFin = Reservation.DateFin;
                DateTime DateDebut2 = new DateTime();
                DateTime DateFin2 = new DateTime();
                foreach (Categorie bloc1 in Autresblocs)
                {
                    Reservations Reservations = ReservationDAO.GetReservationsbloc(bloc1.Id);

                    foreach (Reservation Reservation1 in Reservations)
                    {
                        if (DateDebut.Date == Reservation1.DateDebut.Date && DateFin.Date == Reservation1.DateFin.Date)
                        {
                            if (DateDebut.TimeOfDay <= Reservation1.DateDebut.TimeOfDay && DateFin.TimeOfDay > Reservation1.DateDebut.TimeOfDay)
                            {
                                if (DateFin.TimeOfDay > Reservation1.DateFin.TimeOfDay)
                                {
                                    DateDebut2 = Reservation1.DateDebut;
                                    DateFin2 = Reservation1.DateFin;
                                    DateDebut = Reservation1.DateDebut;
                                    DateFin = Reservation1.DateFin;
                                }
                                else
                                {
                                    DateDebut2 = Reservation1.DateDebut;
                                    DateDebut = Reservation1.DateDebut;
                                    DateFin2 = DateFin;
                                }
                            }

                            else if (DateDebut.TimeOfDay >= Reservation1.DateDebut.TimeOfDay && DateDebut.TimeOfDay < Reservation1.DateFin.TimeOfDay)
                            {
                                if (DateFin.TimeOfDay > Reservation1.DateFin.TimeOfDay)
                                {
                                    DateDebut2 = DateDebut;
                                    DateFin2 = Reservation1.DateFin;
                                    DateFin = Reservation1.DateFin;
                                }
                                else
                                {
                                    DateDebut2 = DateDebut;
                                    DateFin2 = DateFin;

                                }

                            }



                        }

                    }


                }
                if ((DateDebut2 >= Reservation.DateDebut && DateFin2 <= Reservation.DateFin))
                {
                    Reservation Reservation3 = new Reservation();
                    Reservation3.DateDebut = DateDebut2;
                    Reservation3.DateFin = DateFin2;
                    Reservations2.Add(Reservation3);


                }

            }


            return Reservations2;

        }
        public static int SaveReservation(int id, DateTime DateDebut, DateTime DateFin, int ActeId, int CliniqueId, int MedecinId, String body)
        {
            return ReservationDAO.SaveReservation(id, DateDebut, DateFin, ActeId, CliniqueId, MedecinId, body);
        }
        public static Categories GetBlocsDispo(DateTime DateDebut, DateTime DateFin, int CliniqueId, int acteId)
        {

            Categories blocs = CategorieDAO.Getblocs(CliniqueId, acteId);
            Categories blocsdispo = new Categories();
            foreach (Categorie bloc in blocs)
            {
                Reservations Reservations = ReservationDAO.GetReservationsbloc(bloc.Id);
                int i = 0;
                Boolean etat = false;
                while (i < Reservations.Count && etat == false)
                {
                    if (DateDebut.Date == Reservations.ElementAt(i).DateDebut.Date && DateFin.Date == Reservations.ElementAt(i).DateFin.Date)
                    {
                        if (
                            (DateDebut.TimeOfDay <= Reservations.ElementAt(i).DateDebut.TimeOfDay && DateFin.TimeOfDay > Reservations.ElementAt(i).DateDebut.TimeOfDay)
                            ||

                            (DateDebut.TimeOfDay > Reservations.ElementAt(i).DateDebut.TimeOfDay && DateFin.TimeOfDay <= Reservations.ElementAt(i).DateFin.TimeOfDay)
                            ||
                            (Reservations.ElementAt(i).DateDebut.TimeOfDay <= DateDebut.TimeOfDay && Reservations.ElementAt(i).DateFin.TimeOfDay > DateDebut.TimeOfDay)
                           ||
                             (Reservations.ElementAt(i).DateDebut.TimeOfDay > DateDebut.TimeOfDay && Reservations.ElementAt(i).DateFin.TimeOfDay <= DateFin.TimeOfDay)

                            )
                        { etat = true; }

                    }
                    i++;

                }
                if (etat == false)
                {
                    blocsdispo.Add(bloc);
                }


            }

            return blocsdispo;

        }
        public static Boolean IsMedecinFree(DateTime DateDebut, DateTime DateFin, int IdMedecin)
        {
            Reservations Reservations = ReservationDAO.GetReservMedecin(IdMedecin);
            int i = 0;
            Boolean etat = false;
            while (i < Reservations.Count && etat == false)
            {
                if (DateDebut.Date == Reservations.ElementAt(i).DateDebut.Date && DateFin.Date == Reservations.ElementAt(i).DateFin.Date)
                {
                    if (
                        (DateDebut.TimeOfDay <= Reservations.ElementAt(i).DateDebut.TimeOfDay && DateFin.TimeOfDay > Reservations.ElementAt(i).DateDebut.TimeOfDay)
                        ||

                        (DateDebut.TimeOfDay > Reservations.ElementAt(i).DateDebut.TimeOfDay && DateFin.TimeOfDay <= Reservations.ElementAt(i).DateFin.TimeOfDay)
                        ||
                        (Reservations.ElementAt(i).DateDebut.TimeOfDay <= DateDebut.TimeOfDay && Reservations.ElementAt(i).DateFin.TimeOfDay > DateDebut.TimeOfDay)
                       ||
                         (Reservations.ElementAt(i).DateDebut.TimeOfDay > DateDebut.TimeOfDay && Reservations.ElementAt(i).DateFin.TimeOfDay <= DateFin.TimeOfDay)

                        )
                    {
                        etat = true;
                        return false;
                    }

                }
                i++;

            }

            return true;
        }
        public static void AddReservCategorie(int ReservId, int BlocId)
        {
            ReservationDAO.AddReservCategorie(ReservId, BlocId);
        }
        public static Reservations GetReservMedecin(int IdMedecin)
        {
            return ReservationDAO.GetReservMedecin(IdMedecin);
        }
        public static void UpdateReservation(DateTime DateDebut, DateTime DateFin, int ActeId, int CliniqueId, int MedecinId, String body, int id)
        {
            ReservationDAO.UpdateReservation(DateDebut, DateFin, ActeId, CliniqueId, MedecinId, body, id);
        }
        public static Boolean existReserv(int id_reserv)
        {
            return ReservationDAO.existReserv(id_reserv);
        }
        public static int GetLastIndexReserv()
        {
            Reservations Reservations = ReservationDAO.GetAllReservations();
            return Reservations.ElementAt(Reservations.Count - 1).Id;

        }
        public static void DeleteReserv(int reservId)
        {
            ReservationDAO.DeleteReserv(reservId);
        }
        public static Proprietes GetProprietesCategorie(int id_categorie)
        {
            return CategorieDAO.GetProprietesCategorie(id_categorie);
        }
        public static Reservations GetReservMedecinEnCours(int IdMedecin)
        {
            Reservations Reservations = ReservationDAO.GetReservMedecinAll(IdMedecin);
            Reservations ReservationsEncours = new Reservations();
            foreach (Reservation Reserv in Reservations)
            {
                if (Reserv.etat == 0)
                {
                    ReservationsEncours.Add(Reserv);

                }
            }
            return ReservationsEncours;


        }
        public static Reservations GetReservMedecinAccordees(int IdMedecin)
        {
            Reservations Reservations = ReservationDAO.GetReservMedecinAll(IdMedecin);
            Reservations ReservationsAccordees = new Reservations();
            foreach (Reservation Reserv in Reservations)
            {
                if (Reserv.etat == 1)
                {
                    ReservationsAccordees.Add(Reserv);

                }
            }
            return ReservationsAccordees;



        }
        public static Reservations GetReservMedecinRefusees(int IdMedecin)
        {
            Reservations Reservations = ReservationDAO.GetReservMedecinAll(IdMedecin);
            Reservations ReservationsRefusees = new Reservations();
            foreach (Reservation Reserv in Reservations)
            {
                if (Reserv.etat == 2)
                {
                    ReservationsRefusees.Add(Reserv);

                }
            }
            return ReservationsRefusees;

        }

        public static void UpdateReservConfirmee(int idReserv, int idPatient, string RemarquesPatient)
        {
            ReservationDAO.UpdateReservConfirmee(idReserv, idPatient, RemarquesPatient);
        }

        public static int  AddPatient(Patient patient)
        {
            return PatientDAO.AddPatient(patient);
        }

        public static Reservation GetInfoConvocation(int ReservId)
        {
            return PatientDAO.GetInfoConvocation(ReservId);
        }
        

    }
}
